package com.hua.yuan.live.repertory.cloud_data.loader;
import android.support.annotation.NonNull;
import com.alibaba.fastjson.JSONObject;
import com.hua.yuan.common.https.OkHttpModule;
import com.hua.yuan.live.repertory.cloud_data.api.LiveReservationApi;
import com.hua.yuan.live.repertory.cloud_data.res.ResultBean;
import java.io.IOException;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 预约主播
 */
public class LiveReservationLoader {

    private LiveReservationApi mApi;

    private Disposable mPublish;

    public interface onReservationCallback{


        void resevation(Integer isSuccess);

    }
    /**
     * 获取主播的接口数据
     *
     * @param pToken    用户登录的token
     * @param pAnchorId 主播的Id
     */
    public static void reservation(@NonNull final String pToken, @NonNull final int pAnchorId, @NonNull final String startTime,
                            @NonNull final String endTime, @NonNull final String money,
                            @NonNull final onReservationCallback pCallback) {
        // 第一步：初始化Observable
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Integer> pEmitter) throws Exception {
                LiveReservationApi mApi=OkHttpModule.getInstance().getRetrofit().create(LiveReservationApi.class);
                Call<String> call = mApi.reservation(pToken,pAnchorId,startTime,endTime,money);
                try {
                    Response<String> response = call.execute();
                    if (response.body() == null || response.body().equals("")) {
                        pEmitter.onNext(null);
                    }

                    ResultBean resultData = JSONObject.parseObject(response.body(), ResultBean.class);
                    if(resultData==null){
                        pEmitter.onNext(null);
                    }
                    pEmitter.onNext(resultData.getErrorNo());
                } catch (IOException e) {
                    e.printStackTrace();
                    pEmitter.onError(e);
                }


            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Integer>() {
                    private Disposable mPublish;
                    @Override
                    public void onSubscribe(Disposable disposable) {
                        mPublish=disposable;
                    }

                    @Override
                    public void onNext(Integer isSuccess) {
                        pCallback.resevation(isSuccess);
                        mPublish.dispose();
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        mPublish.dispose();
                        pCallback.resevation(-1);
                        throwable.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        mPublish.dispose();
                    }

                });
    }
}
